package com.lanyam.travel.base.entity.base;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lanyam.travel.base.entity.GoodsDate;
import com.lanyam.travel.base.entity.GoodsInfo;
import com.lanyam.travel.base.entity.Product;
import com.lanyam.travel.base.entity.ScenicHotel;
import com.lanyam.travel.base.entity.TravelLine;
import com.lanyam.travel.base.service.IGoodsDateService;
import com.lanyam.travel.base.service.IGoodsInfoService;
import com.lanyam.travel.base.service.IScenicHotelService;
import com.lanyam.travel.base.service.ITravelLineService;
import com.lanyam.travel.base.utils.Constant;

@Component
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
public class MultithreadScheduleTask {
	
	@Autowired
	private ITravelLineService travelLineService;
	
	@Autowired
	private IScenicHotelService scenicHotelService;
	
	@Autowired
	private IGoodsDateService goodsDateService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	/**
	 * 每天创建GoodsDate对象
	 */
	@Async
//	 @Scheduled(fixedDelay = 1000)
//	@Scheduled(cron="0 0/1 * * * ?")
    @Scheduled(cron="0 */15 0 * * ?")  //每天的00：05触发，CronTrigger配置完整格式为： [秒] [分] [小时] [日] [月] [周] [年]
    public void setGoodsDateByDay() {
		System.out.println("setGoodsDateByDay触发了");
        List<GoodsInfo> goodsInfoList = goodsInfoService.getAllGoodsInfo();
        List<ScenicHotel> scenicHotelList = scenicHotelService.getAllScenicHotel();
        List<TravelLine> lineList = travelLineService.getAllTravelLine();
        List<Product> all = new ArrayList<Product>();
        List<ScenicHotel> hotelList = new ArrayList<ScenicHotel>();
        all.addAll(lineList);
        for (ScenicHotel scenic : scenicHotelList) {
			if(scenic.getProductType() == 0)
			{
				all.add(scenic);
			}
			else
			{
				hotelList.add(scenic);
			}
		}
        Calendar today = Calendar.getInstance();
        //把商品日期表更新到30天
  		Calendar calendar = Calendar.getInstance();
  		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		List<GoodsDate> addGoodsDateList = new ArrayList<GoodsDate>();
		//这里增加酒店房间类型的日期价格
        for (GoodsInfo info : goodsInfoList) {
        	int days = Constant.TICKET_DAYS;
        	Date latestDate = goodsDateService.getLatestDate(info.getId());
        	Calendar afterCalendar = Calendar.getInstance();
        	afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
			afterCalendar.set(Calendar.MINUTE, 0);
			afterCalendar.set(Calendar.SECOND, 0);
			afterCalendar.set(Calendar.MILLISECOND, 0);
    		if(null != latestDate )
    		{
    			afterCalendar.setTime(latestDate);//目标日期的时间点
    			afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
    			afterCalendar.set(Calendar.MINUTE, 0);
    			afterCalendar.set(Calendar.SECOND, 0);
    			afterCalendar.set(Calendar.MILLISECOND, 0);
    			days = (int)Math.ceil((afterCalendar.getTimeInMillis() - calendar.getTimeInMillis()) / (1000.0 * 60 * 60 * 24));
    			days = Constant.TICKET_DAYS - 1 - days;
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);//增加最新日期的下一天
    			if(days<0)
    			{
    				days = 0;
    			}
    		}
    		GoodsDate goodsDate;
    		for (int i = 0; i < days; i++) {
    			//增加酒店房间类型对应的日期
    			goodsDate = new GoodsDate();
    			goodsDate.setGoodsId(info.getId());
    			goodsDate.setPrice(info.getDefaultPrice().doubleValue());
    			goodsDate.setPriceDisparity(0);
    			goodsDate.setRemaindNum(info.getDefaultCount());
    			goodsDate.setTheDate(new Timestamp(afterCalendar.getTimeInMillis()));//设置固定的时间为8：00：00
    			goodsDate.setProductType(info.getGoodType());
    			addGoodsDateList.add(goodsDate);
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);
    			//增加酒店对应的差价数据，差价跟酒店id关联，下面单独处理
    		}
    		//删除前第60天的记录，暂时先不删除
//    		today.add(Calendar.DAY_OF_YEAR, -60);
//    		goodsDate = goodsDateService.queryByGoodsIdAndDay(info.getId(), afterCalendar.getTime());
//    		if(null != goodsDate)
//    		{	
//    			goodsDateService.deleteGoodsDateById(goodsDate.getId());
//    		}
		}
        //这里增加景区/线路的日期价格及差价
        for (Product product : all) {
        	int days = Constant.TICKET_DAYS;
        	Date latestDate = goodsDateService.getLatestDate(product.getId());
        	Calendar afterCalendar = Calendar.getInstance();
        	afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
			afterCalendar.set(Calendar.MINUTE, 0);
			afterCalendar.set(Calendar.SECOND, 0);
			afterCalendar.set(Calendar.MILLISECOND, 0);
    		if(null != latestDate )
    		{
    			afterCalendar.setTime(latestDate);//目标日期的时间点
    			afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
    			afterCalendar.set(Calendar.MINUTE, 0);
    			afterCalendar.set(Calendar.SECOND, 0);
    			afterCalendar.set(Calendar.MILLISECOND, 0);
    			days = (int)Math.ceil((afterCalendar.getTimeInMillis() - calendar.getTimeInMillis()) / (1000.0 * 60 * 60 * 24));
    			days = Constant.TICKET_DAYS - 1 - days;
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);//增加最新日期的下一天
    			if(days<0)
    			{
    				days = 0;
    			}
    		}
    		GoodsDate goodsDate;
    		for (int i = 0; i < days; i++) {	
    			goodsDate = new GoodsDate();
    			goodsDate.setGoodsId(product.getId());
    			goodsDate.setPrice(product.getPrice());
    			goodsDate.setPriceDisparity(0);
    			goodsDate.setRemaindNum(product.getTotalNum());
    			goodsDate.setTheDate(new Timestamp(afterCalendar.getTimeInMillis()));//设置固定的时间为8：00：00
    			goodsDate.setProductType(product.getProductType());
    			addGoodsDateList.add(goodsDate);
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);
    		}
    		//删除前第60天的记录
//    		today.add(Calendar.DAY_OF_YEAR, -60);
//    		goodsDate = goodsDateService.queryByGoodsIdAndDay(product.getId(), afterCalendar.getTime());
//    		if(null != goodsDate)
//    		{	
//    			goodsDateService.deleteGoodsDateById(goodsDate.getId());
//    		}
		}
        //这里单独增加酒店的日期差价
        for (ScenicHotel hotel : hotelList) {
        	int days = Constant.TICKET_DAYS;
        	Date latestDate = goodsDateService.getLatestDate(hotel.getId());
        	Calendar afterCalendar = Calendar.getInstance();
        	afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
			afterCalendar.set(Calendar.MINUTE, 0);
			afterCalendar.set(Calendar.SECOND, 0);
			afterCalendar.set(Calendar.MILLISECOND, 0);
    		if(null != latestDate )
    		{
    			afterCalendar.setTime(latestDate);//目标日期的时间点
    			afterCalendar.set(Calendar.HOUR_OF_DAY, 0);
    			afterCalendar.set(Calendar.MINUTE, 0);
    			afterCalendar.set(Calendar.SECOND, 0);
    			afterCalendar.set(Calendar.MILLISECOND, 0);
    			days = (int)Math.ceil((afterCalendar.getTimeInMillis() - calendar.getTimeInMillis()) / (1000.0 * 60 * 60 * 24));
    			days = Constant.TICKET_DAYS - 1 - days;
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);//增加最新日期的下一天
    			if(days<0)
    			{
    				days = 0;
    			}
    		}
    		GoodsDate goodsDate;
    		for (int i = 0; i < days; i++) {
    			goodsDate = new GoodsDate();
    			goodsDate.setGoodsId(hotel.getId());
    			goodsDate.setPrice(0);
    			goodsDate.setPriceDisparity(0);
    			goodsDate.setRemaindNum(0);
    			goodsDate.setTheDate(new Timestamp(afterCalendar.getTimeInMillis()));//设置固定的时间为8：00：00
    			goodsDate.setProductType(hotel.getProductType());
    			addGoodsDateList.add(goodsDate);
    			afterCalendar.add(Calendar.DAY_OF_YEAR, 1);
    		}
    		//删除前第60天的记录
//    		today.add(Calendar.DAY_OF_YEAR, -60);
//    		goodsDate = goodsDateService.queryByGoodsIdAndDay(product.getId(), afterCalendar.getTime());
//    		if(null != goodsDate)
//    		{	
//    			goodsDateService.deleteGoodsDateById(goodsDate.getId());
//    		}
		}
        if(addGoodsDateList.size() > 0)
        {
        	goodsDateService.batchAddGoodsDates(addGoodsDateList);
        }
    }

    @Async
    @Scheduled(cron="0 5 0 1 * ?")//每个月1号的00：05触发
    public void resetGoodsDeatCount() 
    {
    	List<TravelLine> allTravelLine = travelLineService.getAllTravelLine();
    	for (TravelLine travelLine : allTravelLine) {
    		travelLine.setDealCount(0);
    		travelLineService.update(travelLine);
		}
        List<ScenicHotel> allScenicHotel = scenicHotelService.getAllScenicHotel();
    	for (ScenicHotel scenicHotel : allScenicHotel) {
    		scenicHotel.setDealCount(0);
    		scenicHotelService.update(scenicHotel);
		}
    }
}
